package com.mystore.servlet;

import com.mystore.entity.Order;
import com.mystore.entity.User;
import com.mystore.service.GoodService;
import com.mystore.service.OrderService;
import com.mystore.service.impl.GoodServiceImpl;
import com.mystore.service.impl.OrderServiceImpl;
import com.mystore.vo.OrderVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "OrderServlet", value = "/check/order/*")
public class OrderServlet extends HttpServlet {
    OrderService orderService = new OrderServiceImpl();
    GoodService goodService = new GoodServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String method = uri.replace(request.getContextPath() + "/check/order/", "");
        System.out.println("method=" + method);
        PrintWriter out = response.getWriter();
        User user = (User) request.getSession().getAttribute(UserServlet.USER);
        int uid = user.getId();
        HttpSession session = request.getSession();
        String url = request.getContextPath();

        switch (method) {
            case "getOrder"://添加到购物车

                List<OrderVO> list = orderService.findMyOrder(uid);
                request.setAttribute("order", list);
                url = "/WEB-INF/page/order.jsp";
                request.getRequestDispatcher(url).forward(request, response);
                break;
            case "addOrder"://获取我的购物车列表

                int gid = Integer.parseInt(request.getParameter("gid"));
                int num = Integer.parseInt(request.getParameter("num"));
                int address = Integer.parseInt(request.getParameter("address"));
                String status="未发货,等待商家确认";
                //获取发货地址
                String  startAddress= request.getParameter("startAddress");
                Order order = new Order(uid,gid,num,address,status,startAddress);
                try {
                   Boolean isadd =  orderService.addOrder(order);
                   if(isadd){
                       List<OrderVO> list1 = orderService.findMyOrder(uid);
                       url = "/WEB-INF/page/order.jsp";
                       request.setAttribute("order", list1);
                       request.getRequestDispatcher(url).forward(request, response);
                   }else{
                       out.println("生成订单失败,即将前往订单列表");
                       url+="/check/order/getOrder";
                       response.setHeader("refresh", "2;url=" + url);
                   }
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                break;

            case "modOrder"://修改订单
                int id = Integer.parseInt(request.getParameter("oid"));
                String status1 = "订单已结单,已发货";
                Order order1 = new Order(id,uid,status1);
                try {
                   Boolean isup =  orderService.updateOrder(order1);
                   if(isup){
                       out.println("修改成功");
                   }else{
                       out.println("修改订单状态失败");
                   }
                    url+="/check/order/getOrder";
                    response.setHeader("refresh", "2;url=" + url);
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                break;
            default:
                out.println("invalid mark");

        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
